//
//  moneyDetialVC.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/21.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "moneyDetialVC.h"
#import "moneyDetialCell.h"
#import "headView.h"
#import "moneyDetialModel.h"
@interface moneyDetialVC ()
@property (nonatomic, strong) NSMutableArray *titleAry;
@end
@implementation moneyDetialVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"资金明细";
    // 添加所有子控制器
    [self setUpAllViewController];
    // 推荐方式
    [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight, CGFloat *titleWidth) {
        // 设置标题字体
        *titleFont = [UIFont systemFontOfSize:15];
        *titleHeight = 35;
        *selColor = [UIColor custom_color:@"#A91E23"];
    }];
    // 推荐方式（设置下标）
    [self setUpUnderLineEffect:^(BOOL *isUnderLineDelayScroll, CGFloat *underLineH, UIColor *__autoreleasing *underLineColor, BOOL *isUnderLineEqualTitleWidth) {
        // 标题填充模式
        *underLineColor = [UIColor custom_color:@"#A91E23"];
    }];
    __weak moneyDetialVC *ws = self;
    [self setUpContentViewFrame:^(UIView *contentView) {
        contentView.frame = ws.view.frame;
    }];
    
    UIView *grayLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    grayLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:grayLine];
}
- (void)setUpAllViewController {
    customVC *VC1 = [customVC new];
    customVC *VC2 = [customVC new];
    customVC *VC3 = [customVC new];
    customVC *VC4 = [customVC new];
    customVC *VC5 = [customVC new];
    VC1.title = self.titleAry[0];
    VC2.title = self.titleAry[1];
    VC3.title = self.titleAry[2];
    VC4.title = self.titleAry[3];
    VC5.title = self.titleAry[4];
    [self addChildViewController:VC1];
    [self addChildViewController:VC2];
    [self addChildViewController:VC3];
    [self addChildViewController:VC4];
    [self addChildViewController:VC5];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark --- getter
- (NSMutableArray *)titleAry {
    if (!_titleAry) {
        _titleAry = [NSMutableArray array];
        [_titleAry addObjectsFromArray:@[@"全部", @"还款", @"充值", @"提现", @"直接付款"]];
    }
    return _titleAry;
}

@end

@implementation customVC
- (void)viewDidLoad {
    
    headView *view = [[headView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    [self.view addSubview:view];
    CGRect frame = CGRectMake(0, 30, kScreenWidth, kScreenHeight - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 35 - 30);
    self.tableView = [[UITableView alloc]initWithFrame:frame];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addRefreshHeader];
    [self addRefreshFooter];
    [self confingureTableView];
    
    [self loadNew];
}

// MARK: - 下拉刷新
- (void)addRefreshHeader {
    [super addRefreshHeader];
}
- (void)addRefreshFooter {
    [super addRefreshFooter];
}

- (void)loadNew
{
    self.page = 1;
    [self moneyDetialRequest];
}

- (void)loadMore
{
    if (!self.moneyDetialList.count) {
        [self endFooterRefresh];
        return;
    }
    self.page = self.page + 1;
    [self moneyDetialRequest];
}
- (void)confingureTableView {
    // [super confingureTableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([moneyDetialCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([moneyDetialCell class])];
}
- (void)handleResponseData:(NSDictionary *)responseObject
{
    NSMutableArray *modelArray =  [moneyDetialModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"result"]];
    if (self.page == 1) {
        [self.moneyDetialList removeAllObjects];
    }
    if (modelArray.count > 0) {
        [self.moneyDetialList addObjectsFromArray:(NSArray *)modelArray];
        NSString *count = [NSString stringWithFormat:@"%@", responseObject[@"data"][@"pageCount"]];
        if (self.page == count.integerValue) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }else {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}

#pragma mark --- uitableViewDelegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.moneyDetialList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    moneyDetialCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([moneyDetialCell class])];
    cell.model = [self.moneyDetialList objectAtIndex:indexPath.section];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    grayView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return grayView;
}

- (void)moneyDetialRequest {
    NSString *type;
    if ([self.title isEqualToString:@"全部"]) {
        type = @"";
    }else if ([self.title isEqualToString:@"还款"]) {
        type = @"account";
    }else if ([self.title isEqualToString:@"充值"]) {
        type = @"recharge";
    }else if ([self.title isEqualToString:@"提现"]) {
        type = @"cash";
    }else if ([self.title isEqualToString:@"直接付款"]){
        type = @"buyOff";
    }
    NSDictionary *dic = @{
                          @"pager.pageNumber":@(self.page),
                          @"pager.pageSize":@10,
                          @"token":USERTOKEN,
                          @"type":type
                          };
    __weak typeof(self) weakSelf = self;
    [networkManger PostHttpDataWithUrlStr:[[apiManger shareApiManger] moneyDetial] Dic:dic SuccessBlock:^(id responseObject) {
        [weakSelf endRefreshing];
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf handleResponseData:responseObject];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [weakSelf endRefreshing];
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
#pragma mark --- getter
- (NSMutableArray *)moneyDetialList {
    if (!_moneyDetialList) {
        _moneyDetialList = [NSMutableArray array];
    }
    return _moneyDetialList;
}

@end
